package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.R;
import com.example.myapplication.entity.HotTopic;

import java.util.List;

public class TopicAdapter extends RecyclerView.Adapter<TopicAdapter.TopicHolder>{
    private List<HotTopic.HotBean> hotBeanList;
    private Context context;

    public TopicAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<HotTopic.HotBean> hotBeanList){
        this.hotBeanList = hotBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public TopicHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_card,parent,false);
        return new TopicHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TopicHolder holder, int position) {
        HotTopic.HotBean hotBean = hotBeanList.get(position);
        holder.card_title.setText(hotBean.getTitle());
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < hotBean.getText().size(); i++) {
            str.append(hotBean.getText().get(i));
        }
        if(str.length()!=0){
            holder.card_text.setText(str);
        }
        else{
            holder.card_text.setText("点击查看详情");
        }
        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        Glide.with(context).load(hotBean.getSharePicUrl())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_baseline_refresh_24)
                .placeholder(R.drawable.ic_baseline_refresh_24)
                .apply(options)
                .into(holder.card_img);
    }

    @Override
    public int getItemCount() {
        return hotBeanList==null? 0:hotBeanList.size();
    }

    class TopicHolder extends RecyclerView.ViewHolder {
        TextView card_title;
        CardView cardView;
        TextView card_text;
        ImageView card_img;
        public TopicHolder(@NonNull View itemView) {
            super(itemView);
            card_title = itemView.findViewById(R.id.card_title);
            cardView = itemView.findViewById(R.id.cardView);
            card_text = itemView.findViewById(R.id.card_text);
            card_img = itemView.findViewById(R.id.card_img);
        }
    }
}
